package com.gxa.portal.controller;

import com.gxa.common.exception.BusinessException;
import com.gxa.common.result.Result;
import com.gxa.order.dto.OrderDTO;
import com.gxa.order.service.OrderService;
import com.gxa.portal.interceptor.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {


    @Reference
    private OrderService orderService;

    //1.下单
    @PostMapping("/create")
    public Result create(@RequestBody OrderDTO orderDTO, BindingResult bindingResult) throws BusinessException {
        if (bindingResult.hasErrors()) {
            List<String> errors =
                    bindingResult.getAllErrors().stream()
                            .map(ObjectError::getDefaultMessage).
                            collect(Collectors.toList());
            log.error("创建订单参数不正确, orderDTO={}", orderDTO);
            return new Result(5000,"参数不正确");
        }
        String orderId=orderService.createOrder(UserThreadLocal.get().getId(),orderDTO);
        return new Result(orderId);
    }



}
